package com.dulichdanang.weather;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.dulichdanang.R;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class Weather extends ListActivity {

	// All static variables
	static final String URL = "http://weather.service.msn.com/data.aspx?weasearchstr=Da%20Nang,vnm";
	// XML node keys
	static final String KEY_FORECAST = "forecast"; // parent node
	static final String KEY_DATE = "date";
	static final String KEY_DAY = "day";
	static final String KEY_HIGH = "high";
	static final String KEY_LOW = "low";
	static final String KEY_SKYCO = "skycodeday";
	static final String KEY_SKYTE = "skytextday";
	static final String KEY_SHO = "shortday";
	static final String KEY_PRE = "precip";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weather_main);

		ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(URL); // getting XML
		Document doc = parser.getDomElement(xml); // getting DOM element

		NodeList nl = doc.getElementsByTagName(KEY_FORECAST);
		System.out.println(nl.getLength());
		// looping through all item nodes <item>
		for (int i = 0; i < nl.getLength(); i++) {
			// creating new HashMap
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
			// adding each child node to HashMap key => value

			map.put(KEY_DATE, e.getAttribute(KEY_DATE));
			map.put(KEY_SKYCO, e.getAttribute(KEY_SKYCO));
			map.put(KEY_DAY, e.getAttribute(KEY_DAY));
			map.put(KEY_SKYTE, e.getAttribute(KEY_SKYTE));
			map.put(KEY_HIGH, e.getAttribute(KEY_HIGH));
			map.put(KEY_LOW, e.getAttribute(KEY_LOW));
			map.put(KEY_SHO, e.getAttribute(KEY_SHO));
			map.put(KEY_PRE, e.getAttribute(KEY_PRE));
			// adding HashList to ArrayList
			menuItems.add(map);
		}

		// Adding menuItems to ListView
		ListAdapter adapter = new SimpleAdapter(this, menuItems,
				R.layout.weather_list_item, new String[] { KEY_DAY, KEY_DATE, KEY_HIGH,
						KEY_LOW, KEY_SHO,KEY_SKYTE,KEY_SKYCO,KEY_PRE},
				new int[] { R.id.day, R.id.date, R.id.high, R.id.low,R.id.shortday
						,R.id.skytextday,R.id.skycodeday,R.id.precip });

		setListAdapter(adapter);

		// selecting single ListView item
		ListView lv = getListView();

		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// getting values from selected ListItem
				String day = ((TextView) view.findViewById(R.id.day)).getText()
						.toString();
				String date = ((TextView) view.findViewById(R.id.date))
						.getText().toString();

				String low = ((TextView) view.findViewById(R.id.low)).getText()
						.toString();
				String high = ((TextView) view.findViewById(R.id.high))
						.getText().toString();

				String skytextday = ((TextView) view
						.findViewById(R.id.skytextday)).getText().toString();
				String skycodeday = ((TextView) view
						.findViewById(R.id.skycodeday)).getText().toString();
				String shortday = ((TextView) view.findViewById(R.id.shortday))
						.getText().toString();
				String precip = ((TextView) view.findViewById(R.id.precip))
						.getText().toString();

				/*
				 * String skycoday = ((TextView)
				 * view.findViewById(R.id.skyco_label)) .getText().toString();
				 * String skyte = ((TextView)
				 * view.findViewById(R.id.skyte_label)) .getText().toString();
				 * String sho = ((TextView) view.findViewById(R.id.sho))
				 * .getText().toString(); String pre = ((TextView)
				 * view.findViewById(R.id.pre_label)) .getText().toString();
				 */
				// String nhietdo=shortday+skytextday;
				//String chitiet1 = "High:" + high + " " + "low:" + " " + low;
				// Starting new intent
				Intent in = new Intent(getApplicationContext(),
						SingleMenuItemActivity.class);
				in.putExtra(KEY_DAY, day);
				in.putExtra(KEY_HIGH, high);
				in.putExtra(KEY_DATE, date);
				in.putExtra(KEY_LOW, low);
				in.putExtra(KEY_SKYTE, skytextday);
				in.putExtra(KEY_SKYCO, skycodeday);
				in.putExtra(KEY_SHO, shortday);
				in.putExtra(KEY_PRE, precip);
				startActivity(in);

			}
		});
	}
}